﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace community.LanFen
{
    public partial class BrochureAdded : Form
    {

        sql_database database = new sql_database();
        int i = 0;

        public BrochureAdded()
        {
            InitializeComponent();
        }

        private void showData()
        {
            //显示datagridview
            string sqltext = "select ID, publicity_ID as 活动ID , brochure_name as 宣传册名称 ,brochure_account as 数量 ,brochure_content as 内容  from brochure";

            database.show_dataGridview(sqltext, "brochure", dataGridView1);
        }

        //添加下拉选项
        private void addItem()
        {
            string sqltext = "select  SEQID_INT  from  t_cs_healtheducation";
            using (SqlConnection con = new SqlConnection(SQL .conStr ))
            {
                if (con.State == ConnectionState.Closed)
                    con.Open();
                

                //获取职工号
                SqlCommand com = new SqlCommand(sqltext, con);
                SqlDataReader rdr = com.ExecuteReader();

                while (rdr.Read())
                {
                    //string number = Convert.ToInt32(rdr["SEQID_INT"]);
                    parentID_box.Items.Add(Convert.ToInt32(rdr["SEQID_INT"]));
                    //MessageBox.Show(max_number.ToString());
                }
                rdr.Close();
            }

        }

        private void BrochureAdded_Load(object sender, EventArgs e)
        {
            //添加活动ID下拉选项
            addItem();

            //显示datagridview
            string sqltext = "select ID, publicity_ID as 活动ID , brochure_name as 宣传册名称 ,brochure_account as 数量 ,brochure_content as 内容  from brochure";
            
            database.show_dataGridview(sqltext, "brochure", dataGridView1);

            //
        }

        //保存
        private void save_button_Click(object sender, EventArgs e)
        {
            if (i == 1)
            {
                string sqlInsert = "insert into brochure (ID, brochure_name,brochure_account,brochure_content,publicity_ID,addDate_dat) values (" + database.getSerial("brochure", "ID") + ",'";
                sqlInsert += name_Box.Text.Trim() + "','" + account_Box.Text.Trim() + "','" + content_Box.Text.Trim() + "','" + Convert.ToInt32(parentID_box.Text.Trim()) + "', getdate())";
                database.new_row(sqlInsert);
                //MessageBox.Show(sqlInsert);



                i = 0;
            }
            else //修改信息
            {
                string select_row_number = Convert.ToString(dataGridView1[0, dataGridView1.CurrentCell.RowIndex].Value).Trim();

                string sqlText = "update dbo.brochure set brochure_name = '"+name_Box .Text .Trim ()+"' ,brochure_account = '"+account_Box.Text.Trim ();
                sqlText += "' ,brochure_content = '"+content_Box.Text .Trim ()+"',publicity_ID = "+Convert.ToInt32 (parentID_box.Text .Trim ())+",addDate_dat = getdate() where id = " + select_row_number;

                using (SqlConnection  conn = new SqlConnection(SQL.conStr ))
                {
                    if (conn.State == ConnectionState.Closed)
                        conn.Open();
                    SqlCommand com = new SqlCommand(sqlText, conn);

                    int ii = 0;
                    try
                    {
                        ii = com.ExecuteNonQuery();
                        if (ii == 1)
                            MessageBox.Show("恭喜！更新成功！");
                        else
                        {

                            MessageBox.Show("更新失败！");
                        }
                    }
                    catch (Exception ex)
                    {

                        MessageBox.Show(ex.ToString());
                    }
                    i = 0;


                }
            }

            //显示datagridview
            showData();
        }

        //添加内容
        private void add_button_Click(object sender, EventArgs e)
        {
            i = 1;

            name_Box.Text = "";
            account_Box.Text = "";
            content_Box.Text = "";
            parentID_box.Text = "";

        }

        //鼠标点击表格
        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {


            string select_row_number = Convert.ToString(dataGridView1[0, dataGridView1.CurrentCell.RowIndex].Value).Trim();
            if (select_row_number == "")
            {
                return;
            }
            //MessageBox.Show(select_row_number );

            string sqlSelect = "select publicity_ID as 活动ID , brochure_name as 宣传册名称 ,brochure_account as 数量 ,brochure_content as 内容  from brochure where ID = "+ select_row_number ;

            using (SqlConnection  conn = new SqlConnection(SQL .conStr ))
            {
                if (conn.State == ConnectionState.Closed)
                    conn.Open();

               SqlCommand  com = new SqlCommand(sqlSelect, conn);

                try
                {
                    SqlDataReader rdr = com.ExecuteReader();

                    while (rdr.Read())
                    {
                        name_Box.Text = rdr["宣传册名称"].ToString();
                        parentID_box.Text = rdr["活动ID"].ToString();
                        account_Box.Text = rdr["数量"].ToString();
                        content_Box.Text = rdr["内容"].ToString();
                    }
                }catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString ());
                }
            }
        }

        //删除按钮
        private void delete_button_Click(object sender, EventArgs e)
        {
            //是否删除
            if (MessageBox.Show("确定删除改记录吗？", "删除提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.Cancel)
                return;

            //删除操作
            string select_row_number = Convert.ToString(dataGridView1[0, dataGridView1.CurrentCell.RowIndex].Value).Trim();

            string sqlText = "delete from brochure where ID = "+select_row_number ;

            using (SqlConnection  conn = new SqlConnection(SQL.conStr ))
            {
                if (conn.State == ConnectionState.Closed)
                    conn.Open();
                SqlCommand com = new SqlCommand(sqlText, conn);

                int i;
                try
                {
                    i = com.ExecuteNonQuery();
                    if (i == 0)
                        MessageBox.Show("删除失败！");
                }
                catch (Exception ex)
                {

                    MessageBox.Show(ex.ToString());
                }


            }
            //显示datagridview
            showData();
        }


    }
}
